#include<MainWindow.h>

MainWindow::MainWindow()
{
    Jogador1 = new QLabel(tr("Player 1: "));
    Jogador2 = new QLabel(tr("Player 2: "));

    pontosJogador1 = new QLabel("0");
    pontosJogador2 = new QLabel("0");

    JogadorAtual = new QLabel(tr("Turn:" ));;
    nomeJA = new QLabel(tr(" - "));

    status = new QLabel(tr("Status: "));
    statusMsg = new QLabel(tr(" - "));

    statusBar()->addWidget(Jogador1);
    statusBar()->addWidget(pontosJogador1);
    statusBar()->addWidget(Jogador2);
    statusBar()->addWidget(pontosJogador2);
    statusBar()->addWidget(JogadorAtual);
    statusBar()->addWidget(nomeJA);
    statusBar()->addWidget(status);
    statusBar()->addWidget(statusMsg);

    

    gamepanel = new GamePanel();
    connect(gamepanel, SIGNAL(changeTurn(Jogador *)), this, SLOT(updatePlayerTurn(Jogador *)));
    connect(gamepanel, SIGNAL(updateStatusBar(QString)), this, SLOT(updateStatusBarMsg(QString)));
    connect(gamepanel, SIGNAL(updateScore(int, int)), this, SLOT(updateScore(int, int)));

    cw = new ConfigWindow();
    connect(cw, SIGNAL(updateEvalFunc(FuncaoAvaliacao*)), gamepanel, SLOT(updateEvalFunc(FuncaoAvaliacao*)));
    connect(cw, SIGNAL(updateAILevel(int)), gamepanel, SLOT(updateAILevel(int)));
    connect(cw, SIGNAL(updateTRSpeed(int)), gamepanel, SLOT(updateTRSpeed(int)));

    cw->setupConfigs();    
    

    gamepanel->iniciaNovoJogo();

    createMenus();

	
	QPixmap* pixicon = new QPixmap();
	pixicon->load(":/icon.png");
	
	QIcon icon(*pixicon);
	
	setWindowIcon(icon);
    setCentralWidget(gamepanel);
    setWindowTitle("grandOthello! 1.0.0");
    setFixedSize(800, 750);
}

void MainWindow::createMenus()
{
    novoJogo = new QAction(tr("&New Game"), this);
    novoJogo->setShortcut(tr("Ctrl+N"));
    novoJogo->setStatusTip(tr("Start a new game"));
    connect(novoJogo, SIGNAL(triggered()), gamepanel, SLOT(iniciaNovoJogo()));

    sair = new QAction(tr("&Exit"), this);
    sair->setShortcut(tr("Ctrl+Q"));
    sair->setStatusTip(tr("Exit the game"));
    connect(sair, SIGNAL(triggered()), this, SLOT(close()));

    mostraDicas = new QAction(tr("&Show all possible moves"), this);
    mostraDicas->setShortcut(tr("Ctrl+S"));
    mostraDicas->setStatusTip(tr("Show all possible moves that the human player can do"));
    connect(mostraDicas, SIGNAL(triggered()), gamepanel, SLOT(alteraMostraTodasJogadas()));

    configuracoes = new QAction(tr("&Configuration"), this);
    configuracoes->setShortcut(tr("Ctrl+C"));
    configuracoes->setStatusTip(tr("Open the options window"));
    connect(configuracoes, SIGNAL(triggered()), this, SLOT(abreConfiguracoes()));

    /*ajuda = new QAction(tr("&Help"), this);
    ajuda->setShortcut(tr("Ctrl+H"));
    ajuda->setStatusTip(tr("View the game help"));
    connect(ajuda, SIGNAL(triggered()), this, SLOT(abreAjuda()));

    sobre = new QAction(tr("&About"), this);
    sobre->setShortcut(tr("Ctrl+A"));
    sobre->setStatusTip(tr("Litte more about the game and it's creators ;)"));
    connect(sobre, SIGNAL(triggered()), this, SLOT(abreSobre()));*/

    menuJogo = menuBar()->addMenu(tr("&Game"));
    menuJogo->addAction(novoJogo);
    menuJogo->addAction(sair);

    menuConfig = menuBar()->addMenu(tr("&Options"));
    menuConfig->addAction(mostraDicas);
    menuConfig->addAction(configuracoes);

    /*menuGrandOthello = menuBar()->addMenu(tr("&grandOthello"));
    menuGrandOthello->addAction(ajuda);
    menuGrandOthello->addAction(sobre);*/
}

void MainWindow::abreConfiguracoes()
{
    qDebug("Abre a tela de configuracoes");
    cw->show();
}

void MainWindow::abreAjuda()
{
    /* Abre a janela de ajuda */
    qDebug("Abre a tela de ajuda");
}

void MainWindow::abreSobre()
{
    /* Abre a janela de About */
    qDebug("Abre a tela de sobre");
}

void MainWindow::updatePlayerTurn(Jogador *j)
{
    if(j == NULL)return;

    nomeJA->setText(QString::fromStdString(j->getNome()));
    statusBar()->repaint();
}

void MainWindow::updateStatusBarMsg(QString s)
{
    statusMsg->setText(s);
    statusBar()->repaint();
}

void MainWindow::updateScore(int s1, int s2)
{
    pontosJogador1->setText(QString::number(s1));
    pontosJogador2->setText(QString::number(s2));
}
